//
//  XMLoginOtherPhoneView.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/8.
//

#import "XMLoginOtherPhoneView.h"
#import "XMConfirmServiceView.h"

@interface XMLoginOtherPhoneView ()

@property (nonatomic, strong) UITextField *phoneTextField;
//@property (nonatomic, strong) UITextField *codeTextField;
@property (nonatomic, strong) UIButton *getCodeBtn;
@property (nonatomic, strong) UILabel *serviceLabel;

@property (nonatomic, strong) UIButton *selectBtn;

@end

@implementation XMLoginOtherPhoneView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(0, kNavagationBarH - 44, 44, 44) image:@"back_icon_black" selectImg:@"back_icon_black"];
    [backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:backBtn];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(28.0), kNavagationBarH + KScaleWidth(5.0), self.width - KScaleWidth(28.0)*2, KScaleWidth(46.0)) font:KFontSize(32.0) textColor:kColorHEX(0x221232)];
    titleLabel.text = @"手机号登录/注册";
    [self addSubview:titleLabel];
    
    UILabel *explainLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(28.0), titleLabel.maxY + KScaleWidth(5.0), self.width - KScaleWidth(28.0)*2, KScaleWidth(33.0)) font:KFontSize(16.0) textColor:kColorHEX(0x988DA2)];
    explainLabel.text = @"未注册的手机号验证通过后将自动注册";
    [self addSubview:explainLabel];
    
    UIImageView *phoneIcon = [[UIImageView alloc] initWithFrame:CGRectMake(KScaleWidth(28.0), explainLabel.maxY + KScaleWidth(95.0), KScaleWidth(17.0), KScaleWidth(17.0))];
    phoneIcon.contentMode = UIViewContentModeScaleAspectFit;
    phoneIcon.image = ImageNamed(@"login_phone_icon");
    [self addSubview:phoneIcon];
    
    UILabel *phoneTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(phoneIcon.maxX + KScaleWidth(10.0), phoneIcon.y, self.width - phoneIcon.maxX - KScaleWidth(10.0), phoneIcon.height) font:KFontSize(16.0) textColor:kColorHEX(0x988DA2)];
    phoneTitleLabel.text = @"手机";
    [self addSubview:phoneTitleLabel];
    
    UIView *phoneNumBgView = [[UIView alloc] initWithFrame:CGRectMake(KScaleWidth(28.0), phoneIcon.maxY + KScaleWidth(5.0), self.width - KScaleWidth(28.0)*2, KScaleWidth(44.0))];
    phoneNumBgView.backgroundColor = kColorHEX(0xF5F3F7);
    [phoneNumBgView setCornerRadius:phoneNumBgView.height/2];
    [self addSubview:phoneNumBgView];
    
    UIButton *areaSelBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(16.0) frame:CGRectMake(0, 0, KScaleWidth(75.0), phoneNumBgView.height) title:@"+86" titleColor:kColorHEX(0x221232) image:@""];
    [areaSelBtn addTarget:self action:@selector(areaSelAction:) forControlEvents:UIControlEventTouchUpInside];
    [phoneNumBgView addSubview:areaSelBtn];
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(areaSelBtn.maxX, KScaleWidth(8.0), 1, phoneNumBgView.height - KScaleWidth(8.0)*2)];
    lineView.backgroundColor = kColorHEX(0xCBC3D2);
    [phoneNumBgView addSubview:lineView];
    [phoneNumBgView addSubview:self.phoneTextField];
    
    [self addSubview:self.getCodeBtn];
    _getCodeBtn.y = phoneNumBgView.maxY + KScaleWidth(114.0);
    
    [self addSubview:self.selectBtn];
    _selectBtn.y = self.getCodeBtn.maxY + KScaleWidth(27.0);
    
    //服务条款
    UILabel *serviceLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(50.0), _selectBtn.y, self.width - KScaleWidth(50.0), _selectBtn.height)];
    NSMutableAttributedString *serviceAttr = [[NSMutableAttributedString alloc] initWithString:@"登录表示您已同意本平台《服务条款》和《隐私政策》" attributes:@{NSFontAttributeName : KFontSize(13.0)}];
    [serviceAttr setAttributes:@{NSForegroundColorAttributeName : kColorHEX(0x3B5998), NSFontAttributeName : KFontSize(13.0)} range:NSMakeRange(11, 5)];
    [serviceAttr setAttributes:@{NSForegroundColorAttributeName : kColorHEX(0x3B5998), NSFontAttributeName : KFontSize(13.0)} range:NSMakeRange(18, 5)];
    [serviceLabel setAttributedText:serviceAttr];
    [self addSubview:serviceLabel];
    
    CGFloat width = [@"登录表示您已同意本平台" getWidthWithFontSize:KFontSize(13.0)];
    
    UIButton *serviceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    serviceBtn.frame = CGRectMake(serviceLabel.x + width, serviceLabel.y, KScaleWidth(90.0), serviceLabel.height);
    [serviceBtn addTarget:self action:@selector(serviceCheckAction:) forControlEvents:UIControlEventTouchUpInside];
//    serviceBtn.backgroundColor = kColorHEX(0xff00ff);
    [self addSubview:serviceBtn];
    UIButton *privacyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    privacyBtn.frame = CGRectMake(serviceBtn.maxX + KScaleWidth(20.0), serviceBtn.y, serviceBtn.width, serviceBtn.height);
//    privacyBtn.backgroundColor = kColorHEX(0xff00ff);
    [privacyBtn addTarget:self action:@selector(privacyCheckAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:privacyBtn];
    
    UIButton *lookAroundBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(14.0) frame:CGRectMake(KScaleWidth(100.0), self.height - KScaleWidth(100.0) - KScaleWidth(40.0), self.width - KScaleWidth(100.0)*2, KScaleWidth(40.0)) title:@"随便看看" titleColor:kColorHEX(0x333333) image:@"login_next_icon"];
    [lookAroundBtn addTarget:self action:@selector(lookAroundAction:) forControlEvents:UIControlEventTouchUpInside];
    [lookAroundBtn layoutButtonWithEdgeInsetsStyle:CSButtonEdgeInsetsStyleRight ImageTitleSpace:KScaleWidth(7.0)];
    [self addSubview:lookAroundBtn];
    
    UIButton *weLoginBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake((self.width - KScaleWidth(60.0))/2, lookAroundBtn.y - KScaleWidth(60.0) - KScaleWidth(20.0), KScaleWidth(60.0), KScaleWidth(60.0)) image:@"login_wechat_icon" selectImg:@"login_wechat_icon"];
    [weLoginBtn addTarget:self action:@selector(loginWithWe:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:weLoginBtn];
}

#pragma mark - BtnAction

- (void)backAction:(UIButton *)button {
    [self.viewController dismissViewControllerAnimated:YES completion:nil];
}

//服务条款
- (void)serviceCheckAction:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(checkService)]) {
        [self.delegate checkService];
    }
}

//隐私政策
- (void)privacyCheckAction:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(checkPrivacy)]) {
        [self.delegate checkPrivacy];
    }
}

//随便看看
- (void)lookAroundAction:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(lookAround)]) {
        [self.delegate lookAround];
    }
}

//微信登陆
- (void)loginWithWe:(UIButton *)button {
    if (!_selectBtn.selected) {
        XMConfirmServiceView *confirmView = [[XMConfirmServiceView alloc] initWithFrame:self.bounds];
        [self addSubview:confirmView];
        [confirmView show];
        XMWeakSelf;
        confirmView.confirmBlock = ^{
            weakSelf.selectBtn.selected = YES;
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(loginWithWechat)]) {
                [weakSelf.delegate loginWithWechat];
            }
        };
        confirmView.checkServiceBlock = ^(NSString * _Nonnull type) {
            if ([type isEqualToString:@"4"]) {
                [weakSelf serviceCheckAction:nil];
            }
            else {
                [weakSelf privacyCheckAction:nil];
            }
        };
        return;
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(loginWithWechat)]) {
        [self.delegate loginWithWechat];
    }
}

- (void)selectAction:(UIButton *)button {
    button.selected = !button.selected;
}

- (void)areaSelAction:(UIButton *)button {
    
}

- (void)getCodeAction:(UIButton *)button {
    if (!_selectBtn.selected) {
        XMConfirmServiceView *confirmView = [[XMConfirmServiceView alloc] initWithFrame:self.bounds];
        [self addSubview:confirmView];
        [confirmView show];
        XMWeakSelf;
        confirmView.confirmBlock = ^{
            weakSelf.selectBtn.selected = YES;
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(getCodeWithPhoneNum:)]) {
                [weakSelf.delegate getCodeWithPhoneNum:weakSelf.phoneTextField.text];
            }
        };
        confirmView.checkServiceBlock = ^(NSString * _Nonnull type) {
            if ([type isEqualToString:@"4"]) {
                [weakSelf serviceCheckAction:nil];
            }
            else {
                [weakSelf privacyCheckAction:nil];
            }
        };
        return;
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(getCodeWithPhoneNum:)]) {
        [self.delegate getCodeWithPhoneNum:_phoneTextField.text];
    }
}

- (void)inputChanged:(UITextField *)textField {
    _getCodeBtn.enabled = [textField.text validateMobile];
}

#pragma mark - Init
- (UITextField *)phoneTextField {
    if (!_phoneTextField) {
        _phoneTextField = [[UITextField alloc] initWithFrame:CGRectMake(KScaleWidth(85.0), 0, self.width - KScaleWidth(28.0)*2 - KScaleWidth(85.0), KScaleWidth(44.0))];
        _phoneTextField.font = KFontSize(16.0);
        _phoneTextField.textColor = kColorHEX(0x221232);
        _phoneTextField.placeholder = @"请输入手机号码";
        [_phoneTextField addTarget:self action:@selector(inputChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _phoneTextField;
}

- (UIButton *)getCodeBtn {
    if (!_getCodeBtn) {
        _getCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(16.0) frame:CGRectMake(KScaleWidth(28.0), 0, self.width - KScaleWidth(28.0)*2, KScaleWidth(44.0)) title:@"获取短信验证码" titleColor:kColorHEX(0xCBC3D2)];
       _getCodeBtn.backgroundColor = kColorHEX(0xF5F3F7);
       [_getCodeBtn setCornerRadius:_getCodeBtn.height/2];
       [_getCodeBtn setBackgroundImage:[UIImage imageWithColor:kColorHEX(0xF5F3F7)] forState:UIControlStateDisabled];
        [_getCodeBtn setTitleColor:kColorHEX(0xCBC3D2) forState:UIControlStateDisabled];
        [_getCodeBtn setBackgroundImage:[UIImage imageWithColor:kColorHEX(0xFFD800)] forState:UIControlStateNormal];
        [_getCodeBtn setTitleColor:kColorHEX(0x333333) forState:UIControlStateNormal];
       [_getCodeBtn addTarget:self action:@selector(getCodeAction:) forControlEvents:UIControlEventTouchUpInside];
        _getCodeBtn.enabled = NO;
    }
    return _getCodeBtn;
}

- (UILabel *)serviceLabel {
    if (!_serviceLabel) {
        _serviceLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(28.0), 0, self.width - KScaleWidth(28.0)*2, KScaleWidth(33.0)) font:KFontSize(16.0) textColor:kColorHEX(0x988DA2)];
    }
    return _serviceLabel;
}

- (UIButton *)selectBtn {
    if (!_selectBtn) {
        _selectBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(KScaleWidth(23.0), 0, KScaleWidth(26.0), KScaleWidth(26.0)) image:@"login_service_select_icon" selectImg:@"login_service_select_icon_selected"];
        [_selectBtn addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selectBtn;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
